package com.cloud.bc.service.impl;

import com.cloud.bc.mapper.BcFinancesMapper;
import com.cloud.bc.model.entity.BcFinanceRecords;
import com.cloud.bc.mapper.BcFinanceRecordsMapper;
import com.cloud.bc.model.entity.BcFinances;
import com.cloud.bc.service.BcFinanceRecordsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.common.model.enums.CurrencyTypeEnum;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

/**
 * 财务记录
 *
 * @author douhaichao code generator
 * @date 2021-01-13 11:19:13
 */
@Service
@AllArgsConstructor
public class BcFinanceRecordsServiceImpl extends ServiceImpl<BcFinanceRecordsMapper, BcFinanceRecords> implements BcFinanceRecordsService {

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcFinanceRecords selectOne(BcFinanceRecords req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcFinanceRecords>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcFinanceRecords> selectAll(BcFinanceRecords req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcFinanceRecords> selectPage(Page page, BcFinanceRecords req) {
        QueryWrapper<BcFinanceRecords> bcFinanceRecordsQueryWrapper = new QueryWrapper<>();
        bcFinanceRecordsQueryWrapper.setEntity(req);
        bcFinanceRecordsQueryWrapper.orderByDesc("id");
        IPage<BcFinanceRecords> list = baseMapper.selectPage(
                page, bcFinanceRecordsQueryWrapper
        );
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcFinanceRecords> selectPageByLike(Page page, BcFinanceRecords req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (null != req.getCurrencyType()) {
            queryWrapper.eq("currency_type", req.getCurrencyType());
        }

        if (null != req.getType()) {
            queryWrapper.eq("type", req.getType());
        }

        if (null != req.getPid()) {
            queryWrapper.eq("pid", req.getPid());
        }

        if (null != req.getIncome()) {
            queryWrapper.eq("income", req.getIncome());
        }

        if (null != req.getSurplus()) {
            queryWrapper.eq("surplus", req.getSurplus());
        }

        if (!StrUtil.isEmpty(req.getRemark())) {
            queryWrapper.like("remark", req.getRemark());
        }

        if (null != req.getCreateTime()) {
            queryWrapper.eq("create_time", req.getCreateTime());
        }
        IPage<BcFinanceRecords> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcFinanceRecords req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    private final BcFinancesMapper bcFinancesMapper;

    @Override
    public boolean recodeAdd(BcFinanceRecords req) {
        //查询用户余额并记录操作后金额
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", req.getUserId());
        BcFinances bcFinances = bcFinancesMapper.selectOne(queryWrapper);
        if (bcFinances == null) {
            //如果账户是空的，就在这里创建一个钱包
            bcFinancesMapper.insert(new BcFinances().setUserId(req.getUserId()));
            bcFinances = new BcFinances();
            bcFinances.setCandyPrice(BigDecimal.ZERO);
            bcFinances.setMoney(BigDecimal.ZERO);
        }
        if (req.getCurrencyType().equals(CurrencyTypeEnum.CASH.getCode())) {
            req.setSurplus(bcFinances.getMoney());
        } else if (req.getCurrencyType().equals(CurrencyTypeEnum.CANDY.getCode())) {
            req.setSurplus(bcFinances.getCandyPrice());
        }
        baseMapper.insert(req);
        return true;
    }
}
